#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
double CalculateArea(double a, double b, double c)
{
	double s = (a + b + c) / 2;
	return sqrt(s * (s - a) * (s - b) * (s - c));
}
void FindMaxTriangle(const vector<double>& lengths)
{
	double MaxPerimeter = 0, MaxArea = 0;
	double a1, a2, b1, b2, c1, c2;
	a1 = a2 = b1 = b2 = c1 = c2 = 0;
	vector<double> sort_lengths = lengths;
	sort(sort_lengths.begin(), sort_lengths.end());
	for (int i = lengths.size() - 1; i >= 2; i--) {
		double a = sort_lengths[i];
		double b = sort_lengths[i - 1];
		double c = sort_lengths[i - 2];
		if (a < b + c) {
			double perimeter = a + b + c;
			double area = CalculateArea(a, b, c);
			if (perimeter > MaxPerimeter) {
				MaxPerimeter = perimeter;
				a1 = c; b1 = b; c1 = a;
			}
			if (area > MaxArea) {
				MaxArea = area;
				a2 = c; b2 = b; c2 = a;
			}
		}
	}
	if (MaxPerimeter > 0) {
		cout << "构成的最大三角形周长为：" << MaxPerimeter << "（选择" << a1 << "," << b1 << "," << c1 << "）" << endl;
		cout << "构成的最大三角形面积为：" << MaxArea << "（选择" << a2 << "," << b2 << "," << c2 << "）" << endl;
	}
	else {
		cout << "无法构成三角形。" << endl;
	}
}
int main()
{
	int m;
	do {
		cout << "请输入木棍的数目 m = ";
		cin >> m;
		if (m < 3) 	cout << "木棍数目必须大于等于3。" << endl;
	} while (m < 3);
	vector<double> lengths(m);
	cout << "请输入" << m << "根木棍长度：";
	for (int i = 0; i < m; i++) {
		cin >> lengths[i];
	}
	FindMaxTriangle(lengths);
	system("pause");
	
	return 0;
}